package vip.xiaonuo.core.util;
  
import com.qcloud.cos.COSClient;  
import com.qcloud.cos.ClientConfig;  
import com.qcloud.cos.auth.BasicCOSCredentials;  
import com.qcloud.cos.auth.COSCredentials;  
import com.qcloud.cos.exception.CosClientException;  
import com.qcloud.cos.exception.CosServiceException;  
import com.qcloud.cos.model.*;  
import com.qcloud.cos.region.Region;  
  
import java.io.File;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Random;  
  
  
/**  
 * 腾讯云对象存储工具类  
 *  
 */public class TencentCosUtil {  
  
    //bucket的区域  
 private static final String bucket = "ap-chengdu";
    // 此处填写的存储桶名称  
 private static final String bucketName = "family-1258865768";
    // secretId  
 private static final String secretId = "AKID3CFypgWD62sgeVKCCByYYLBB3vgGiAXu";
    // secretKey  
 private static final String secretKey = "BD3fCshMvMqKh095mGgz62wpw175320l";
  
    // 1 初始化用户身份信息(secretId, secretKey，可在腾讯云后台中的API密钥管理中查看！  
 private static COSCredentials cred = new BasicCOSCredentials(secretId,secretKey);  
  
    // 2 设置bucket的区域, COS地域的简称请参照  
 // https://cloud.tencent.com/document/product/436/6224，根据自己创建的存储桶选择地区  
 private static ClientConfig clientConfig = new ClientConfig(new Region(bucket));  
  
  
    private static String FilePath = "img";

    /**
     * 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20 M 以下的文件使用该接口 大文件上传请参照 API 文档高级 API 上传
     *
     * @param localFile
     */ public static String uploadInputfile(InputStream inputStream,String name) throws CosClientException, CosServiceException {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;  //注意加1
        int day = calendar.get(Calendar.DATE);

        // 生成cos客户端
        COSClient cosclient = new COSClient(cred, clientConfig);
        String fileName = name;
        try {
            String substring = fileName.substring(fileName.lastIndexOf("."));
            Random random = new Random();
            // 指定要上传到 COS 上的路径
            //random.nextInt(10000) + System.currentTimeMillis()或者UUID
            fileName = FilePath+"/"+year+"/"+month+"/"+day+"/"+ random.nextInt(10000) + System.currentTimeMillis()+ substring;
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName , inputStream,new ObjectMetadata());
            PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭客户端(关闭后台线程)
            cosclient.shutdown();
        }
        return "https://family-1258865768.cos.ap-chengdu.myqcloud.com/"+fileName;
    }

    /**  
 * 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20 M 以下的文件使用该接口 大文件上传请参照 API 文档高级 API 上传  
 *  
 * @param localFile  
 */ public static String uploadfile(File localFile) throws CosClientException, CosServiceException {  
        Calendar calendar = Calendar.getInstance();  
        int year = calendar.get(Calendar.YEAR);  
        int month = calendar.get(Calendar.MONTH)+1;  //注意加1  
 int day = calendar.get(Calendar.DATE);  
  
        // 生成cos客户端  
 COSClient cosclient = new COSClient(cred, clientConfig);  
        String fileName = localFile.getName();  
        try {  
            String substring = fileName.substring(fileName.lastIndexOf("."));  
            Random random = new Random();  
            // 指定要上传到 COS 上的路径  
 //random.nextInt(10000) + System.currentTimeMillis()或者UUID  
 fileName = FilePath+"/"+year+"/"+month+"/"+day+"/"+ random.nextInt(10000) + System.currentTimeMillis()+ substring;  
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName , localFile);  
            PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            // 关闭客户端(关闭后台线程)  
 cosclient.shutdown();  
        }  
        return "https://family-1258865768.cos.ap-chengdu.myqcloud.com/"+fileName;
    }  
  
  
    /**  
 * * @Title: downFile  
 * @Description: 下载文件  
 * @return  
 */  
 public static void downFile() {  
        // 生成cos客户端  
 COSClient cosclient = new COSClient(cred, clientConfig);  
        //要下载的文件路径和名称  
 String key = "down/demo1.jpg";  
        // 指定文件的存储路径  
 File downFile = new File("src/test/resources/mydown.txt");  
        // 指定要下载的文件所在的 bucket 和对象键  
 GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);  
        ObjectMetadata downObjectMeta = cosclient.getObject(getObjectRequest, downFile);  
    }  
  
  
    /**  
 * 删除文件  
 *  
 * @param key  
 */ public static void deletefile(String key) throws CosClientException, CosServiceException {  
        // 生成cos客户端  
 COSClient cosclient = new COSClient(cred, clientConfig);  
        // 指定要删除的 bucket 和路径  
 cosclient.deleteObject(bucketName, key);  
        // 关闭客户端(关闭后台线程)  
 cosclient.shutdown();  
    }

}
